home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
NeXTSTEP 3.3 (Developer)…68k, x86, SPARC, PA-RISC]
/
NeXTSTEP 3.3 Dev Intel.iso
/
usr
/
sybase
/
sample
/
dblibrary
/
example4.c
< prev
next >
Wrap
C/C++ Source or Header
|
1993-04-22
|
5KB
|
232 lines
/*
** example4.c
**
** This example accesses the data within each row without using dbbind(),
** and illustrates the use of row buffering.
**
** It runs a query, saves all of the returned rows (up to a maximum
** of 1000) using DB-Library row buffering, and allows the user to
** examine data rows at random.
*/
#include <stdio.h>
#include <sybfront.h>
#include <sybdb.h>
extern char *malloc();
DBPROCESS *dbproc; /* Our connection with SQL Server. */
LOGINREC *login; /* Our login information. */
#define TYPELEN 2
/* Forward declarations of the error handler and message handler. */
int err_handler();
int msg_handler();
main(argc, argv)
int argc;
char *argv[];
{
/* Here are the variables which will be used to store the
* data being examined.
*/
DBCHAR name[MAXNAME+1]; /* MAXNAME is defined in
* "sybdb.h" as the maximum
* length for names of database
* objects, such as tables,
* columns, and procedures.
*/
DBCHAR type[TYPELEN+1];
DBINT id;
DBCHAR datebuf[64];
DBINT len;
char numstring[32];
int quitflag = 0;
RETCODE row_code;
int rownum;
/* Initialize DB-Library. */
if (dbinit() == FAIL)
exit(ERREXIT);
/* Install the user-supplied error-handling and message-handling
* routines. They are defined at the bottom of this source file.
*/
dberrhandle(err_handler);
dbmsghandle(msg_handler);
/*
** Get a LOGINREC structure and fill it with the necessary
** login information.
*/
login = dblogin();
DBSETLPWD(login, "server_password");
DBSETLAPP(login, "example4");
dbproc = dbopen(login, NULL);
dbcmd(dbproc, "select name, type, id, crdate from sysobjects");
dbcmd(dbproc, " where type = 'S'");
/*
** Buffer the rows so we can read them any number of times.
** Passing '0' as the number of rows to buffer will get us
** default row buffering (currently 1000 rows).
** If more than 1000 rows are received, this program will
** only save the last 1000.
**
** Note that this parameter must be passed as an ASCII string.
*/
dbsetopt(dbproc, DBBUFFER, "0");
dbsqlexec(dbproc);
if (dbresults(dbproc) == SUCCEED)
{
/* Read all of the rows into DB-Library's buffer */
while ((row_code = dbnextrow(dbproc)) != NO_MORE_ROWS)
{
/* If DB-Library's row buffer is full, throw
* away the oldest row, to allow the newest
* row to be read in.
*/
if (row_code == BUF_FULL)
dbclrbuf(dbproc, 1);
}
/* Print out the column headers. */
printf
(" %-20s %10s %25s %15s \n",
" NAME ",
"TYPE",
" DATE ",
"ID");
printf
(" %20s %10s %25s %15s \n",
"--------------------",
"----",
"----------------------",
"----");
/* Let the user view any row in the table. */
printf("Type the number of the row you want to see.\n");
printf("The first row is number 1.\n");
printf("Asking for row 0 will terminate the program.\n");
while (quitflag == 0)
{
printf("Row number: ");
gets(numstring);
rownum = atoi(numstring);
if (rownum == 0)
quitflag = 1;
else
{
/* Print out the requested row. */
if (dbgetrow(dbproc, rownum) == NO_MORE_ROWS)
printf
("That row is not in the table.\n");
else
{
/* Copy variable-length character data
* (colname).
*/
strncpy
(name, (DBCHAR *)dbdata(dbproc, 1),
(len = dbdatlen(dbproc, 1)));
/* String needs terminating null. */
name[len] = '\0';
/* Copy fixed-length character data. */
strncpy
(type, (DBCHAR *)dbdata(dbproc, 2),
(len = dbdatlen(dbproc, 2)));
type[len] = '\0';
/* Copy integer data. */
id = *((DBINT *)dbdata(dbproc, 3));
/* Convert datetime data to a printable
* string.
*/
dbconvert(dbproc, SYBDATETIME, (dbdata(dbproc, 4)),
(DBINT)-1, SYBCHAR, datebuf, (DBINT)-1);
printf
("%20s %10s %25s %15ld \n",
name, type, datebuf, id);
}
}
}
}
dbexit();
exit(STDEXIT);
}
int err_handler(dbproc, severity, dberr, oserr, dberrstr, oserrstr)
DBPROCESS *dbproc;
int severity;
int dberr;
int oserr;
char *dberrstr;
char *oserrstr;
{
if ((dbproc == NULL) || (DBDEAD(dbproc)))
return(INT_EXIT);
else
{
printf("DB-Library error:\n\t%s\n", dberrstr);
if (oserr != DBNOERR)
printf("Operating-system error:\n\t%s\n", oserrstr);
return(INT_CANCEL);
}
}
int msg_handler(dbproc, msgno, msgstate, severity, msgtext,
srvname, procname, line)
DBPROCESS *dbproc;
DBINT msgno;
int msgstate;
int severity;
char *msgtext;
char *srvname;
char *procname;
DBUSMALLINT line;
{
printf ("Msg %ld, Level %d, State %d\n",
msgno, severity, msgstate);
if (strlen(srvname) > 0)
printf ("Server '%s', ", srvname);
if (strlen(procname) > 0)
printf ("Procedure '%s', ", procname);
if (line > 0)
printf ("Line %d", line);
printf("\n\t%s\n", msgtext);
return(0);
}